/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.table;

import java.awt.Color;
import java.util.Locale;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import jpcap.packet.Packet;
import tnviet.proj.jna.JNAnalyzer;
import tnviet.proj.jna.JNCaptor;
import tnviet.proj.jna.JNPacketAnalyzerLoader;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.analyzer.JNPacketAnalyzer;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class JNTableModel extends AbstractTableModel{


    // Vector of JNTableView - a pair consist of a analyzer and a field name.
    Vector views;
    JNCaptor jNCaptor;
    JNPacketAnalyzer[] analyzers = JNPacketAnalyzerLoader.getAnalyzers();
    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);
    boolean isApplyColorRules= false;
    ProtocolColor[] protocolColors = ProtocolColor.LoadProtocolColors();

    public JNTableModel(JNCaptor jNCaptor,Vector views) {
        this.jNCaptor = jNCaptor;
        this.views = views;
    }

    public int getRowCount() {
        return jNCaptor.getPackets().size();
    }

    public int getColumnCount() {
        return views.size() + 2;
    }

    public void updateProtocolColors(){
        ProtocolColor[] tempProtocolColors = ProtocolColor.LoadProtocolColors();
        for(int i = 0; i< protocolColors.length; i++){
            protocolColors[i].setColor(tempProtocolColors[i].getColor());
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if(jNCaptor.getPackets().size() <= rowIndex) return "";
        Packet packet = (Packet)jNCaptor.getPackets().elementAt(rowIndex);
        if(columnIndex == 0) return new Integer(rowIndex);
        if(columnIndex == 1){
            for(int i=analyzers.length-1;i>=0;i--){
                if(analyzers[i].isAnalyzable(packet)){
                    String protocolName = analyzers[i].getProtocolName();
                    if(isApplyColorRules){
                        return protocolColors[i];
                    }
                    return analyzers[i].getProtocolName();
                }
            }
            return resourceManager.getString("other");
        }
        JNTableView view = (JNTableView)views.elementAt(columnIndex-2);
        JNPacketAnalyzer analyzer = view.getAnalyzer();
        if(analyzer.isAnalyzable(packet)){
            synchronized(analyzer){
                analyzer.analyze(packet);
                Object obj = analyzer.getValue(view.getValueName());
                if (obj instanceof Vector){
                    if(((Vector)obj).size()>0){
                        return ((Vector)obj).elementAt(0);
                    } else {
                        return null;
                    }
                } else {
                    return obj;
                }
            }
        } else {
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public String getColumnName(int column) {
        if(column == 0) return resourceManager.getString("index");
        if(column ==1) return resourceManager.getString("protocol");
        return ((JNTableView)views.elementAt(column-2)).getValueName();
    }

    @Override
    public Class getColumnClass(int c){
        if(c == 0) return Integer.class;
        if(c == 1){
            if(isApplyColorRules) return ProtocolColor.class;
            else return String.class;
        }

        for(int i=2; i<this.getRowCount();i++){
            if(this.getValueAt(i, c) != null) return this.getValueAt(i, c).getClass();
        }
        return String.class;
    }

    public void setIsApplyColorRules(boolean isApplyColorRules) {
        this.isApplyColorRules = isApplyColorRules;
        this.fireTableStructureChanged();
    }

}
